﻿
using Microsoft.AspNetCore.SignalR;
using SignalRApplication.Contracts;

namespace PoemGame.SignalR.Backend
{
    public class MyTimer
    {
        private IHubCallerClients clients;
        private Timer timer;
        private string gameguid;
        private IServiceScopeFactory serviceProviderFactory;
        public MyTimer(IHubCallerClients clients, string gameguid)
        {
            this.clients = clients;
            this.gameguid = gameguid;
        }

        public MyTimer(IHubCallerClients clients,
            string gameguid,
            IServiceScopeFactory serviceProviderFactory
            )
        {
            this.clients = clients;
            this.gameguid = gameguid;
            this.serviceProviderFactory = serviceProviderFactory;
        }

        /// <summary>
        /// 设置抢答倒计时,30秒无人抢答，游戏结束
        /// </summary>
        /// <param name="caller"></param>
        public void StartGoToPlayTimer()
        {
            //this.caller = caller;
            var c = 60;
            TimerCallback tc = new TimerCallback(async (o) =>
            {

                //var game =await gameAppService.GetGame(Guid.Parse(gameguid));

                await clients.Group(gameguid).SendAsync("SetCount", c--);
                if (c <= 0)
                {

                    var t = timer.Change(-1, 10000);
                    using (var scope = serviceProviderFactory.CreateScope())
                    {
                        var _gameAppService = scope.ServiceProvider.GetService<ISignalRAppService>();

                        await _gameAppService.GameOver(Guid.Parse(gameguid));
                        await ShowGameBack(clients, _gameAppService, gameguid, "没人抢答，游戏结束");
                    }
                    //await caller.SendAsync("OnGoToPlayTimeOut");
                    StopTimer();
                }
            }
            );
            if (timer != null) StopTimer();
            timer = new Timer(tc, null, 1, 1000);
        }

        /// <summary>
        /// 设置游戏倒计时
        /// </summary>
        /// <param name="caller"></param>
        public void StartPlayTimer(string userName)
        {
            var c = 60;
            TimerCallback tc = new TimerCallback(async (o) =>
            {
                await clients.Group(gameguid).SendAsync("SetCount", c--);
                if (c <= 0)
                {
                    var t = timer.Change(-1, 10000);
                    using (var scope = serviceProviderFactory.CreateScope())
                    {
                        var _gameAppService = scope.ServiceProvider.GetService<ISignalRAppService>();

                        await _gameAppService.SetPlayerOut(userName, Guid.Parse(gameguid));
                        await ShowGameBack(clients, _gameAppService, gameguid, userName + "超时");
                    }
                    //await caller.SendAsync("OnTimeOut");
                    StopTimer();
                }
            }
            );
            if (timer != null) StopTimer();
            timer = new Timer(tc, null, 1, 1000);
        }

        public void StopTimer()
        {
            if (timer != null)
            {
                timer.Dispose();
                timer = null;
            }
        }

        protected async Task ShowGameBack(IHubCallerClients Clients, ISignalRAppService _gameAppService, string gameguid, string message)
        {
            var game = await _gameAppService.GetGame(Guid.Parse(gameguid));
            await Clients.Group(gameguid).SendAsync("ShowGameResult", game, message);
        }
    }
}
